<?php
/**
 * Created by PhpStorm.
 * User: huyang
 * Date: 2017/6/7
 * Time: 下午6:03
 *
 * admin  养悦源 后台管理登录系统
 */
class Account extends Web_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Manager_model', 'manager');
        $this->load->library('session');

    }

    public function index() {
        $this->login();
    }

    public function login() {
        $data['base_url'] = $this->config->item('base_url');
        $this->load->view('admin/account_login',$data);
    }
    public function logout()
    {
        session_destroy();
        $this->session->unset_userdata('admin_web_user');
        $this->login();
    }

    // -------------------api 接口---------------
    public function token() {
        $username = $this->input->get('username');
        $password = $this->input->get('password');
        $captcha = $this->input->get('captcha');



        if (empty($username) || empty($password) || empty($captcha)) {
            $this->response(array('errcode' => 10001));
            return;
        }

        // 验证验证码
        $cap_word = $this->session->cap_word;
        if ($captcha != $cap_word) {
            $this->response(array('errcode' => 1, 'errmsg' => '验证码错误'));

        }

        // 密码验证
        $row = $this->manager->confirmPassword($username, $password);
        if (is_null($row)) {
            $this->response(array('errcode' => 2, 'errmsg' => '账户或密码错误'));
        }

        //删除session
        $this->session->unset_userdata('cap_word');

        // 存储 uid
        $this->session->admin_web_user = (object) array('uid' => $row->uid);

        // 更新ip 和 dateline
        $dateline = strtotime(date('Y-m-d H:i:s'));
        $ip = $_SERVER["REMOTE_ADDR"];
        $this->manager->updateDateline($row->uid, $dateline, $ip);

        $this->response(array('errcode' => 0, 'errmsg' => $this->session->admin_web_user));
    }

    // 发送验证码
    public function captcha() {
        $base_url = $this->config->item('base_url');
        $this->load->helper('config_captcha');
        $this->load->helper('captcha');

        $img_path =  './static/captcha/';
        $img_url = $base_url . 'static/captcha/';
        $cfg = config_captcha($img_path, $img_url);
        $cap = create_captcha($cfg);

        $this->load->library('session');
        $this->session->cap_word = $cap['word'];
        echo $cap['filename'];
    }


}
?>